	<?php
	ob_end_clean();
	ob_start();
?>
<html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<meta http-equiv="Content-Script-Type" content="text/javascript" />
		<meta http-equiv="Content-Style-Type" content="text/css" />

	</head>
<body>
<?php
	require_once( '../system/smarty.inc' );
	require_once( '../system/mdao/Class_M_SHOP.php' );
	require_once( '../system/mdao/Class_M_OTHER.php' );
	require_once( '../system/Class_FILE_CSV.php' );
	require_once( '../system/Class_ERROR.php' );
	require_once( '../system/login.inc.php' );
	require_once( '../system/aoki_m.php' );
	require_once( '../system/Class_PWD.php' );
	
	Class_PWD::valid_auth(AOKI_AUTH_SHOP,$login['staff_auth']);
	$docsv_import = FALSE;
	
	$csvf = Class_FILE_CSV::get_upload_file();
	
	if ($csvf) {
		// ロケール情報の変更
		$locale = setlocale(LC_ALL, 0);
		setlocale(LC_ALL,'ja_JP.UTF-8');
		
		$rowall = Class_FILE_CSV::fget_rows($csvf);
		
		if  (!empty($rowall)) {
			$col_heads = array(
			'shop_cd'         , //店舗コード',      1),
			'shop_name'       , //店舗名',          1),
			'block_cd'        , //地域ブロックコード',1),
			''                , //地域ブロック名',    1),block_name
			'local_cd'        , //都道府県コード',    1),
			''                , //都道府県名',        1),local_name
			'postcode'        , //郵便番号',          1),
			'address'         , //住所',              1),
			'tel'             , //電話番号',          1),
			'lnews1'          , //地域新聞名１コード',1),
			''                , //地域新聞名１名称',  1),lnews1_name
			'lnews2'          , //地域新聞名３コード',1),
			''                , //地域新聞名２名称',  1),lnews2_name
			'lnews3'          , //地域新聞名３コード',1),
			''                , //地域新聞名３名称',  1),lnews3_name
			'lnews4'          , //地域新聞名４コード',1),
			''                , //地域新聞名４名称',  1),lnews4_name
			'lnews5'          , //地域新聞名５コード',1),
			''                , //地域新聞名５名称',  1),lnews5_name
			'memo'            , //備考',  1),
			'year_sale'       , //年商(億円)',  1),
			'stop_date'       , //廃止日',            3),
			'zone_aja_cd'     , //ゾーンAJAコード',  1),
			''                , //ゾーンAJA名',  1),
			'area_aja_cd'     , //エリアAJAコード',  1),
			''                , //エリアAJA名',  1),
			''                , //更新日時',          3),u_time
			''                , //登録日時',          3),c_time
			''                  //更新者',            1)m_user_name
			);
		//	$csvheads = array('shop_cd','shop_name','parent_shop_cd','block_cd','local_cd','postcode','address','tel','smx','center','lnews1','lnews2','lnews3','lnews4','lnews5','stop_date');
			$csvheads  = array();
			$col_index = array();
			foreach ($col_heads as $k => $v) {
				if (!empty($v)) {
					$csvheads[$k]  = $v;
					$col_index[$v] = $k;
				}
			}
			
			unset( $csvheads[$col_index["zone_aja_cd"]]);
			unset( $csvheads[$col_index["area_aja_cd"]] );
			
			$index_block_cd        = $col_index['block_cd'];      //
			$col_block_cd          = $index_block_cd + 1;
			
			$index_local_cd        = $col_index['local_cd'];      //
			$col_local_cd          = $index_local_cd + 1;
			
			$index_zone_aja        = $col_index['zone_aja_cd'];      //
			$col_zone_aja          = $index_zone_aja + 1;
			
			$index_area_aja        = $col_index['area_aja_cd'];      //
			$col_area_aja          = $index_area_aja + 1;
			
		/*
			$index_smx    = $col_index["smx"];
			$col_smx      = $index_smx + 1;
		*/	
			$index_center = $col_index["center"];
			$col_center   = $index_center + 1;
			
			$key1 = $col_index['shop_cd'];//キーです、必ず仕様します
			
			$err = new Class_ERROR("","");
			$dao = new Class_M_SHOP;
			
			$ngline  = 0;
			$error_times = 0;
			$error_stop = IMPORT_STOP_MAX_ERROR_TIMES;
			$errtext = array();
			$isng = FALSE;
			
			$chk     = $dao->get_checks();
			$chklens = $dao->get_maxlens();
			
			$block_arr   = Class_M_OTHER::get_block_array();
			$local_arr   = Class_M_OTHER::get_local_array();
			$media_arr   = Class_M_OTHER::get_pattern_array(NULL, 23);
			
			$zoneareauser  = Class_M_OTHER::get_ajaeks_array(NULL, NULL,TRUE);
			$zoneuser  = array();
			$areauser = array();
			foreach ($zoneareauser as $k =>$v){
				if ($v['auth'] == '2') {
					$areauser[ $v["login_id"] ] =  $v["id"];
				} else {
					$zoneuser[ $v["login_id"] ] =  $v["id"];
				}
			}
		
			if ($login_auth == 'G-2' ) {
				reset($areauser); 
				$areauser = array( $login['staff_id'] => $login['autoid'] );
			}else if ($login_auth == 'G-3' ) {
				reset($zoneuser); 
				$zoneuser = array( $login['staff_id'] => $login['autoid'] );
			}

			
			$linecount = count($rowall);
			$percentold = 100;
 			foreach ($rowall as $line => $row) {
				$isng = FALSE;
				$percent = intval($line/$linecount * 100)."%";
				if  ( $percent != $percentold ) {
					Class_FILE_CSV::write_msg_checking_per( $percent ) ;
				}
				$percentold = $percent;
				foreach ($rowall as $kk=>$vv) {
					if  ($line != $kk) {
						if ($row[$key1] === $vv[$key1]){
							$col = $key1 + 1;
							$isng = TRUE;
							$ngMsg_tmp[$line][] = "{$line}行と{$kk}行の{$col}列目の値重複不可。";
							$error_times++;
							if ($error_times > $error_stop) {
								break;
							}
						}
					}
				} 
				//普通チェック
				foreach ( $csvheads as $k=>$v ) {
					if (isset($chk[$v])) {
						$msg =  $err->check( $rowall[$line][$k], $chk[$v ] ); 
						if (!empty($msg)) {
							$isng = TRUE;
							$col  = $k+1;
							$ngMsg_tmp[$line][] = "{$line}行{$col}列目：{$msg}。";
							$error_times++;					
							if ($error_times > $error_stop) {
								break;
							}
						}
					}
				}
				//桁数チェック
				foreach ( $chklens as $k => $v ) {
					if (isset($col_index[$k])){
						$msg = $err->check_size($rowall[$line][$col_index[$k]], $v);
						if (!empty($msg)) {
							$isng = TRUE;
							$col  = $col_index[$k]+1;
							$ngMsg_tmp[$line][] = "{$line}行{$col}列目：{$msg}。";
							$error_times++;					
							if ($error_times > $error_stop) {
								break;
							}
						}
					}
				}
				if (isset($col_index[$k])){
					$msg = $err->check_size_equal($row[$key1], 3);
					if (!empty($msg)) {
						$isng = TRUE;
						$col  = $key1+1;
						$ngMsg_tmp[$line][] = "{$line}行{$col}列目：{$msg}。";
						$error_times++;					
						if ($error_times > $error_stop) {
							break;
						}
					}
				}
				
				//府県
				if ( !empty($row[$index_local_cd])){
					if (empty($local_arr[$row[$index_local_cd]])) {
						$isng = TRUE;
						$error_times++;
						$ngMsg_tmp[$line][] = "{$line}行{$col_local_cd}列目：都道府県は存在しません。</br>";
						if ($error_times > $error_stop) {
							break;
						}
					}
				} 
				
				//ブロック
				if ( !empty($row[$index_block_cd])){
					if (empty($block_arr[$row[$index_block_cd]])) {
						$isng = TRUE;
						$error_times++;
						$ngMsg_tmp[$line][] = "{$line}行{$col_block_cd}列目：ブロックは存在しません。</br>";
						if ($error_times > $error_stop) {
							break;
						}
						
					}
				} 
				/*
				//smx チェック
				if ( !empty($row[$index_smx]) ) {
					if (!array_key_exists($row[$index_smx], $aoki_set_flags )) {
						$isng = TRUE;
						$error_times++;	
						$ngMsg_tmp[$line][] = "{$line}行{$col_smx}列目：は1または9ではありません。";
					} 
				}
				
				//center チェック
				if ( !empty($row[$index_center]) ) {
					if (!array_key_exists($row[$index_center], $aoki_set_flags )) {
						$isng = TRUE;
						$error_times++;	
						$ngMsg_tmp[$line][] = "{$line}行{$col_center}列目：は1または9ではありません。";
						if ($error_times > $error_stop) {
							break;
						}
					} 
				}*/
				//エリア
				if (empty($areauser[$row[$index_area_aja]])) {
					$isng = TRUE;
					$error_times++;
					$ngMsg_tmp[$line][] = "{$line}行{$col_area_aja}列目：{$row[$index_area_aja]}エリアユーザは存在しません。</br>";
				}
				
				//ゾーン
				if (empty($zoneuser[$row[$index_zone_aja]])) {
					$isng = TRUE;
					$error_times++;
					$ngMsg_tmp[$line][] = "{$line}行{$col_zone_aja}列目：{$row[$index_zone_aja]}ゾーンユーザは存在しません。</br>";
				}
				
				//媒体
				for ($i = 1; $i <= 5; $i++){
					$index_news = $col_index["lnews".$i] ;
					$col_news   = $index_news + 1;
					if ( !empty($row[$index_news])){
						if (empty($media_arr[$row[$index_news]])) {
							$isng = TRUE;
							$error_times++;
							$ngMsg_tmp[$line][] = "{$line}行{$col_news}列目：パッタンは存在しません。</br>";
							if ($error_times > $error_stop) {
								break;
							}
						}
					} 
				}
				
				$lnews = array( "lnews1"=>"地域新聞1" , "lnews2"=>"地域新聞2", "lnews3"=>"地域新聞3", "lnews4"=>"地域新聞4", "lnews5"=>"地域新聞5" );
				foreach ($lnews as $k=>$v) {
					$index_news = $col_index[$k] ;
					if (!empty( $row[$index_news] )) {
						foreach ($lnews as $kk=>$vv) {
							$index_other = $col_index[$kk] ;
							if ( ($row[$index_news] == $row[$index_other]) && ($k != $kk) ) {
								$isng = TRUE;
								$error_times++;
								$err_col1 = $index_news + 1;
								$err_col2 = $index_other + 1;
								$ngMsg_tmp[$line][] = "{$line}行 {$err_col1}列と{$err_col2}列重複している </p";
								break;
							}
						}
					}else {
						$rowall[$line][$index_news] = "0";
					}
				}
				
				//for (i)lnews1
				if ( $isng ) {
					++$ngline;
					$errtext[$ngline] = $row;
					if ($error_times > $error_stop) {
						break;
					}
				}
			}
			if ($ngline > 0) {
				$errHtml = Class_FILE_CSV::format_msg($ngMsg_tmp);
				if ($error_times > $error_stop) {
					Class_FILE_CSV::write_msg_error( $errHtml ,$error_stop );	
				
				} else {
					Class_FILE_CSV::write_msg_error( $errHtml );	
				}
			} else {
				//DBへ更新
				try {
					$daodb = $dao->get_db();
					$dao->begin_trans();
					$i = 0;
					$percent = "";
					$percentold = "";
					foreach ( $rowall as $k=>$v ) {
						$i++;
						$percent = intval($i/$line * 100)."%";
						if  ( $percent != $percentold ) {
							Class_FILE_CSV::write_msg_doing( $percent ) ;
						}
						$percentold = $percent;
						$values  = array();
						
						foreach ( $csvheads as $kk => $vv ) {
							$values[$vv] = $v[$kk];
						}
						$values["zone_aja"] = $zoneuser[$v[$index_zone_aja]];
						$values["area_aja"] = $areauser[$v[$index_area_aja]];
						//$rowall[$line][$index_area_aja] = $areauser[$v[$index_area_aja]];
						
						$keyvalues =  $values[$col_heads[$key1]];
						$where = sprintf(" {$col_heads[$key1]} = '%s'", $keyvalues );
						
						//$values["media_cd"]        =  Class_M_OTHER::do_media($values["media_name"], $daodb);
						//$values["delive_cd"]       =  Class_M_OTHER::do_delive($values["delive_name"], $daodb);
						$values["block_name"]     =  $block_arr[$values["block_cd"]];
						$values["local_name"]     =  $local_arr[$values["local_cd"]];
						$values["stop_date"]      =  Class_PWD::date_to_int8 ( $values['stop_date'] );
						
						if ( $dao->exits( $where ) ) {
							if (!($dao->editbycol($keyvalues ,$values ,$col_heads[$key1] ))) {
								throw new Exception("mshop_import db更新");
							}
						} else {
							if ( !( $dao->add($values ) ) ) {
								throw new Exception("mshop_import db更新");
							}
						}
					}
					Class_FILE_CSV::write_msg_succed($line, "mshop");
					$dao->commit_trans();
					
				} catch (Exception $e) {
					$dao->rollback_trans();
					Class_FILE_CSV::write_msg_error( "DB Exceptionがありました。</br>" );
				}
				
			} 
		}else {
			$errHtml = "CSVファイルエラー";
			Class_FILE_CSV::write_msg_error( $errHtml );
		}
		
		// ロケール情報の復元
		setlocale(LC_ALL, $locale);
	
	}else {

		$errHtml = "CSVファイルエラー";
		Class_FILE_CSV::write_msg_error( $errHtml );
	}
?>
</body>
	</himl>
	